package com.xtayfjpk.security;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAKeyGenParameterSpec;

public class KeyFactoryTest {

	public static void main(String[] args) throws Exception {
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		
		AlgorithmParameterSpec algorithmParameterSpec = new RSAKeyGenParameterSpec(1024, new BigInteger("3"));
		generator.initialize(algorithmParameterSpec);
		
		KeyPair keyPair = generator.generateKeyPair();
		
		byte[] keyBytes = keyPair.getPrivate().getEncoded();
		System.out.println(keyPair.getPrivate().getFormat());
		
		PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
		//X509EncodedKeySpec spec2 = new X509EncodedKeySpec(keyBytes);
		KeyFactory factory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = factory.generatePrivate(spec);
		System.out.println(keyPair.getPrivate().equals(privateKey));
	}

}
